/*
 * @author: G.Piraux, M-A. Taupenot
 * @version 1.0.1
 * date: 21/05/2012
 */
package tests;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.*;

import subscribersManagement.Subscriber;

import java.util.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import jpaUtils.JPAUtil;
import loansManagement.Loan;

import booksManagement.Book;
import booksManagement.CopyDAO;
import booksManagement.DVD;
import booksManagement.Copy;
import exceptions.*;

/**
 * @author        user
 */
public class CopyTest
{
	/**
   * @uml.property  name="b"
   * @uml.associationEnd  
   */
	private Book b;
	
	/**
   * @uml.property  name="s"
   * @uml.associationEnd  
   */
	private Subscriber s;
	
	/**
   * @uml.property  name="copyDAO"
   * @uml.associationEnd  
   */
	private CopyDAO copyDAO;
	/**
   * @uml.property  name="d"
   * @uml.associationEnd  
   */
	private DVD d;
	/**
   * @uml.property  name="c1"
   * @uml.associationEnd  
   */
	private Copy c1;
	/**
   * @uml.property  name="c2"
   * @uml.associationEnd  
   */
	private Copy c2;
	private ArrayList<String> authors;
	
	@Before
	public void beforeTest() throws BadParametersException{
		EntityManager em;
		EntityTransaction tx;
		copyDAO = new CopyDAO();
		
		em= JPAUtil.getEntityManager();
		tx=em.getTransaction();
		tx.begin();
		
		authors = new ArrayList<String>();
		authors.add("Corral");
		b = new Book("Noche", authors, new GregorianCalendar(), "026765415");
		d = new DVD("Nemesis",new GregorianCalendar(),"1237860");
		c1 = b.addCopy();
		c2 = d.addCopy();
		copyDAO.add(c1);
		copyDAO.add(c2);
		
		tx.commit();
		JPAUtil.closeEntityManager();
	}
	
	@Test
	public void testEqualsObject() throws BadParametersException{
		//two copies of the same media are not the same
		Copy c3 = b.addCopy();
		assertTrue(!(c1.equals(c3)));
		assertTrue(c1.equals(c1));
	}
	
	@Test
	public void testIslent() throws BadParametersException, 
	TooManyLoansException, LentBookException, ReservedCopyException{
		new Loan(c1,s);

		assertTrue(c1.isLent());
		
		c1.returnCopy();
		assertFalse(c1.isLent());
	}

}
